package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.model.UserControl;
import com.zzgele.qmzb.api.service.UserControlService;
import com.zzgele.qmzb.api.service.UserGuardService;
import com.zzgele.qmzb.api.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
* Created by Martin on 2017/07/09.
*/
@RestController
@RequestMapping("/assist")
public class AssistController {
    private final Logger logger = LoggerFactory.getLogger(AssistController.class);
    @DubboConsumer
    private UserGuardService userGuardService;
    @DubboConsumer
    private UserControlService userControlService;
    @DubboConsumer
    private UserService userService;

    @PostMapping("/list")
    public Result assist_list(Long login_uid) {
        HashMap map = new HashMap();
        map.put("uid", login_uid);
        List<HashMap> userGuardList = userGuardService.getList(map);
        List<HashMap> userControlList = userControlService.getList(map);

        HashMap hashMap = new HashMap();
        hashMap.put("guard_list", userGuardList);
        hashMap.put("control_list", userControlList);

        return ResultGenerator.genSuccessResult(hashMap);
    }

    @PostMapping("/control_create")
    public Result control_create(Long login_uid, Long control_uid) {
        User userRefer = userService.findBy("id", control_uid);
        if (userRefer == null) {
            return ResultGenerator.genFailResult("对象不存在或已禁用");
        }

        Condition condition = new Condition(UserControl.class);
        condition.createCriteria().andCondition("uid=" + login_uid + " AND control_uid=" + control_uid);
        List<UserControl> userControlList = userControlService.findByCondition(condition);
        if (userControlList.size() != 0) {
            return ResultGenerator.genSuccessResult("重复添加成功！");
        }

        UserControl userControl = new UserControl();
        userControl.setUid(login_uid);
        userControl.setControlUid(control_uid);
        userControlService.save(userControl);

        return ResultGenerator.genSuccessResult("添加成功");
    }

    @PostMapping("/control_delete")
    public Result control_delete(Long login_uid, Long control_uid) {

        Condition condition = new Condition(UserControl.class);
        condition.createCriteria().andCondition("uid=" + login_uid + " AND control_uid=" + control_uid);

        int count = userControlService.deleteByCondition(condition);

        return ResultGenerator.genSuccessResult("删除成功");
    }
}
